;(function(window,$){
    var infobj=null;

    $(function(){
        // just a datepicker
        $('input.datepicker').datepicker({
            language:'zh-CN',
            format:"yyyy-mm-dd"
        });
        getinfo();

        //when a file upload,get the oss url to hidden input

        $('input[type="file"]').on('change',function(){
            var fileInput=$(this);

            util.fileUpload(fileInput[0].files[0],function(res){
                fileInput.prev('span').text('点击重新上传');
                fileInput.next('input[type="hidden"]').val(res.data);
            })
        });

        $('.uploadbox').hover(function(){
            var imgUrl=$(this).find("input[type='hidden']").val();
            if (imgUrl!=="")
            {
                $(this).find("img").attr("src",imgUrl);
                $(this).find("a").attr("href",imgUrl).show();;
            }
        },function(){
            $(this).find("a").hide();
        });

        $('#savebtn').on('click',saveinfo)
    });

    function saveinfo(){
        if (!submitCheck()){
            return;
        }

        // get all text
        $('input[type="text"]').each(function(){
            var key=$(this).attr('name');
            var val=$(this).val();
            infobj[key]=val;
        });

        // get all select
        $('select').each(function(){
            var key=$(this).attr('name');
            var val=$(this).val();
            infobj[key]=val;
        });

        // get all radio
        $('input[type="radio"]:checked').each(function(){
            var key=$(this).attr('name');
            var val=$(this).val();
            infobj[key]=val;
        });

        infobj.avatar=$('input[type="hidden"][name="avatar"]').val();

        //delete some pro
        
        delete infobj.id
        delete infobj.status
        delete infobj.userType
        delete infobj.phone
        delete infobj.score
        delete infobj.successCount
        delete infobj.auditStatus
        
        console.log(infobj)

        util.ajax({
            url:'/manager/profile/save',
            type:'POST',
            data:infobj,
            success:function(res){
                console.log(res);
                util.flushUserInfo(function(){
                    window.location.href="/channel/myprofile.html";
                });
            }
        })

    }

    function getinfo(){
        util.ajax({
            type:'GET',
            url:'/user/manager',
            success:function(res){
                infobj=res.data

                $('input[name="name"]').val(infobj.name);
                $('input[name="telPhone"]').val(infobj.telPhone);
                $('input.datepicker').datepicker('setDate',new Date(infobj.birthday));
                $('input[name="avatar"]').val(infobj.avatar);

                $('input[name="marryType"][value="'+infobj.marryType+'"]').prop('checked','checked');
                $('input[name="sex"][value="'+infobj.sex+'"]').prop('checked','checked');

                $('select[name="provinceId"]').val(infobj.provinceId);
                $('select[name="cityId"]').val(infobj.cityId);

                $('span#user-name').text(infobj.name);
                $('span#user-company').text(infobj.companyName);
                $('span#profile-status').text(parseInt(infobj.isProfile)===1?'已认证':'未认证');
                $('img#headshow').attr('src',infobj.avatar);
            }
        })
    }

    function formatDate(timestamp){
        var date=new Date(timestamp);
        return date.getFullYear+'-'+parseInt(date.getMonth()+1)+'-'+date.getDate();
    }

    // screen 0 to 1 check
    function submitCheck(){
        if ($("input[name='name']").val()==='')
        {
            $("input[name='name']").css("border-color","#FF0000");
            return false
        };

        if ($("input[name='birthday']").val()==='')
        {
            $("input[name='birthday']").css("border-color","#FF0000");
            return false
        };

        if ($("input[name='telPhone']").val()==='')
        {
            $("input[name='telPhone']").css("border-color","#FF0000");
            return false
        };

        return true;
    }

})(window,jQuery)